/**
 * @author caojj
 * @version 1.0.2.0
 */
try{XN}catch(ex){XN={}};
try{getHostSuffix}catch(ex){getHostSuffix=null};

if (!getHostSuffix) {
	function getHostSuffix(){
		var matcher = top.location.host.match(/\.(.*\..*)/i);
		hostSuffix = matcher && matcher[1];
		return hostSuffix || "d1xn.com";
	}
}

XN.ADLoader = function(xmlDom, swfTypeId) {
	this.namespace = "";
	this.jsADs=[];
	this.swfADs=[];
	
	this.xmlDom = xmlDom;
	this.swfTypeId = swfTypeId;
	
	if (typeof(swfTypeId) == "string") {
		swfTypeId = swfTypeId.split();
	}
	
//	if (xmlDom) {
		this.init();
//	}
}
XN.ADLoader.swfADLoadSucc = function() {
	XN.ADLoader.loaded = true;	
}
XN.ADLoader.prototype = {
	init: function(xmlDom) {
		this.parseData();
	},
	parseData : function() {
		var namespace = this.xmlDom.documentElement.getAttribute("namespace");
		this.namespace = namespace || "";
		var Ads = [];
		var groups = this.xmlDom.selectNodes("//group");
		for (var i=0;i<groups.length;i++) {
			var group = groups[i];
			var groupId = group.getAttribute("type");
			var groupName = group.getAttribute("name");
			var items = group.selectNodes(".//item");
			if (items.length>1 || this.checkSwfId(groupId)) {	//flash
				var aData = [];
				for (var j=0;j<items.length;j++) {
					aData.push(this.parseItem(items[j]));
				}
				this.swfADs[groupId] = {
					id : groupId, 
					data : aData
				}
//				this.swfADs.push({
//					id : groupId,
//					data : aData
//				})
			} else if (items.length>0) {	//jpg
				this.jsADs[groupId] = {
					id : groupId, 
					data : [this.parseItem(items[0])]
				}
//				this.jsADs.push({
//					id : groupId,
//					data : this.parseItem(items[0])
//				});
			}
		}
	},
	dispose : function() {
		delete this.xmlDom;
	},
	parseItem : function(itemXmlDom) {
		var url = itemXmlDom.getAttribute("url");
		var desc = itemXmlDom.selectSingleNode(".//desc").text;
		var link = itemXmlDom.selectSingleNode(".//link").text;
		var thumb = "";
		
		if (!url) {url=""}
		if (url.indexOf("http:")<0) {			
			thumb = "http://www."+getHostSuffix()+"/res/public/commonRes/advertisement/"+this.namespace+"/p" + url.split(".")[0] + ".jpg";
			url = "http://www."+getHostSuffix()+"/res/public/commonRes/advertisement/"+this.namespace +"/" + url;
		}
		return {
			url:url,
			desc:desc,
			link:link,
			thumb:thumb
		}
	},
	checkSwfId : function(id) {
		if (!this.swfTypeId) {
			return false;
		}
		
		for (var i=0; i<this.swfTypeId.length; i++) {
			if (this.swfTypeId[i].toString() == id.toString()) {
				return true;
			}
		}
		return false;
	},
	renderTo : function(id, cot) {
		var retDom = null;
		var ad = this.jsADs[id] || this.swfADs[id];
		if (ad) {
			retDom = document.createElement("a");
			retDom.href = ad.data[0].link;
			retDom.title = ad.data[0].desc;
			retDom.target = "_blank";
			
			var img = document.createElement("img");
			img.src = ad.data[0].url;
			img.border = 0;
			
			retDom.appendChild(img);
		}
		
		if (typeof(cot)=="string") {
			cot = document.getElementById(cot);
		}
		
		if (cot&&ad) {
			cot.appendChild(retDom);
		}
		return retDom;
	},
	renderSwf : function(id, swf) {
		if (typeof(swf) == "string") {
			swf = document[swf];
		}
		
		var ad = this.swfADs[id] || this.jsADs[id];
		
		if (swf && ad) {
			try {
				var timer = 0;
				var _tm = setInterval(function() {
					if (XN.ADLoader.loaded || timer>5) {
						clearInterval(_tm);
						try {
							swf.setADData(ad);
						}catch(ex){}						
					} else {
						timer++
					}
				},255);
//				swf.setADData(ad);
			} catch(ex) {}
		}
	},
	getJsAd : function() {
		return this.jsADs;
	},
	getSwfAd : function() {
		return this.swfADs;
	}
	//,
//	$success : function() {}
}

